<?php
/*
*员工接口类
*/
class staffAppClass extends appBase{
	/*列表页-部门*/
	public function department(){
		//数据准备
		$department = array();
		$sql = find(array("table" => "department","where" => "order by list"));
		foreach($sql['array'] as $array){
			$department[$array['type']][] = array(
				"id" => $array['id'],//部门ID
				"name" => $array['name'],//部门名称
			);
		}
		$json['department'] = $this->jsonFormat(array("array" => $department));
		//返回
		$json['warn'] = "success";
		return $json;
	}
	/*列表页-查询条件*/
	public function search(){
		$json['search'] = array(
			//排序方式
			"list" => array(
				array("name" => "会计账户降序"),
				array("name" => "结算账户降序"),
				array("name" => "入职日期降序"),
			),
			//性别
			"sex" => array(
				array("name" => "不限"),
				array("name" => "男"),
				array("name" => "女"),
			),
			//购买社保
			"socialSecurity" => array(
				array("name" => "不限"),
				array("name" => "是"),
				array("name" => "否"),
			),
			//是否有办公室钥匙
			"gateKey" => array(
				array("name" => "不限"),
				array("name" => "是"),
				array("name" => "否"),
			),
		);
		//返回
		$json['warn'] = "success";
		return $json;
	}
	/*列表页-根据部门ID返回员工列表*/
	public function home(){
		if(!$this->power("staff","see")){
			$json['warn'] = "您没有查询员工档案的权限";
		}else{
			//like查询数组
			$like = array(
				"name" => $this->post['name'],//姓名
			);
			//全等查询数组
			$equal = array(
				"sex" => $this->post['sex'],//性别
				"socialSecurity" => $this->post['socialSecurity'],//社保
				"gateKey" => $this->post['gateKey'],//钥匙
			);
			//前置条件
			$and = "";
			//部门
			if(!empty($this->post['departmentId'])){
				$and .= " and jobId in ( select id from job where departmentId = '".$this->post['departmentId']."')";
			}
			//生成查询语句
			$where = $and.search(array("like" => $like,"=" => $equal));
			//排序
			$orderBy = $this->orderBy(array(
				"search" => $this->post['orderBy'],
				"default" => "入职日期降序",
				"array" => array(
					"money" => array("desc" => "会计账户降序",),
					"lastMoney" => array("desc" => "结算账户降序"),
					"entryTime" => array("desc" => "入职日期降序"),
				),
			));
			//获取查询结果
			$page = $this->page(array("table" => "staff","where" => $where,"order" => $orderBy['orderBy']));
			//数据分析
			$json['staff'] = array();
			foreach($page['array'] as $array){
				$job = query("job","id = '$array[jobId]'");
				$department = query("department","id = '$job[departmentId]'");
				//返回
				$json['staff'][] = array(
					"id" => $array['stid'],//员工ID
					"name" => $array['name'],//姓名
					"basePay" => $job['basePay'],//基本工资
					"subsidy" => $job['subsidy'],//岗位津贴
					"tel" => $array['tel'],//手机号码
					"department" => $department['type']."~".$department['name']."~".$job['name'],//职位
					"ico" => headImg($array['sex'],$array['ico']),//头像
				);
			}
			//分页信息
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*详情页*/
	public function detail(){
		$staff = query("staff","stid = '".$this->post['id']."'");
		if(!$this->power("staff","see")){
			$json['warn'] = "您没有查询员工档案的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "员工ID为空";
		}elseif(empty($staff['stid'])){
			$json['warn'] = "未找到此员工";
		}else{
			//基本参数
			$job = query("job","id = '$staff[jobId]'");
			$role = query("role","id = '$job[roleId]'");
			$department = query("department","id = '$job[departmentId]'");
			//直属经理
			$manager = query("staff","stid = '$staff[manager]'");
			$managerJob = query("job","id = '$manager[jobId]'");
			$managerDepartment = query("department","id = '$managerJob[departmentId]'");
			//工作经历
			$jobHistory = array();
			$jobHistorySql = find(array("table" => "jobHistory","where" => "stid = '$staff[stid]' order by dayStart desc limit 10"));
			foreach($jobHistorySql['array'] as $array){
				$jobHistory[] = array(
					"id" => $array['id'],//工作经历ID
					"companyName" => $array['companyName'],//公司名称
					"position" => $array['position'],//所在职位
					"dayStart" => $array['dayStart']."~".$array['dayEnd'],//工作时间段
					"content" => $array['content'],//工作内容
					"cause" => $array['cause'],//离职原因
				);
			}
			//返回
			$json['staff'] = array(
				"name" => $staff['name'],//员工姓名
				"job" => $department['type']."~".$department['name']."~".$job['name'],//职位
				"sex" => $staff['sex'],//性别
				"entryTime" => $staff['entryTime'],//入职日期
				"state" => $staff['state'],//状态
				"socialSecurity" => $staff['socialSecurity'],//社保
				"gateKey" => $staff['gateKey'],//办公室大门钥匙
				"tel" => $staff['tel'],//手机号码
				"qq" => $staff['qq'],//QQ号码
				"stid" => $staff['stid'],//员工ID
				"loginName" => $staff['loginName'],//用户名
				"manager" => $managerDepartment['type']."~".$managerDepartment['name']."~".$managerJob['name']."~".$manager['name'],//直属上级
				"school" => $staff['school'],//毕业院校
				"schoolMajor" => $staff['schoolMajor'],//所学专业
				"schoolEnd" => $staff['schoolEnd'],//毕业日期
				"bankName" => $staff['bankName'],//银行名称
				"bankNum" => $staff['bankNum'],//银行卡号
				"socialSecurityNum" => $staff['socialSecurityNum'],//社保号
				"entryTime" => $staff['entryTime'],//入职日期
				"dingtalkId" => $staff['dingtalkId'],//钉钉员工ID
				"text" => $staff['text'],//备注
				"ico" => root.$staff['ico'],//头像
				"IDCardFront" => root.$staff['IDCardFront'],//身份证正面
				"IDCardBack" => root.$staff['IDCardBack'],//身份证背面
				"diploma" => root.$staff['diploma'],//毕业证扫描件
				"bankIco" => root.$staff['bankIco'],//银行卡扫描件
				"jobHistory" => $jobHistory,//工作经历
			);
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*员工工作经历详情页*/
	public function jobHistory(){
		$jobHistory = query("jobHistory","id = '".$this->post['id']."'");
		if(!$this->power("staff","see")){
			$json['warn'] = "您没有查询员工档案的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "员工工作经历ID为空";
		}elseif(empty($jobHistory['id'])){
			$json['warn'] = "未找到此记录";
		}else{
			$json['detail'] = array(
				"id" => $jobHistory['id'],//工作经历ID
				"stid" => $jobHistory['stid'],//员工ID
				"companyName" => $jobHistory['companyName'],//公司名称
				"position" => $jobHistory['position'],//所在职位
				"content" => $jobHistory['content'],//工作内容
				"cause" => $jobHistory['cause'],//离职原因
				"dayStart" => $jobHistory['dayStart'],//开始日期
				"dayEnd" => $jobHistory['dayEnd'],//结束日期
			);
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*详情页-会计账户-全部*/
	public function accountAll(){
		return $this->account(array("where" => " and type = '员工会计账户'"));
	}
	/*详情页-会计账户-收入*/
	public function accountIn(){
		return $this->account(array("where" => " and type = '员工会计账户' and direction = '收入'"));
	}
	/*详情页-会计账户-支出*/
	public function accountOut(){
		return $this->account(array("where" => " and type = '员工会计账户' and direction = '支出'"));
	}
	/*详情页-结算账户-全部*/
	public function settleAccountAll(){
		return $this->account(array("where" => " and type = '员工结算账户'"));
	}
	/*详情页-结算账户-收入*/
	public function settleAccountIn(){
		return $this->account(array("where" => " and type = '员工结算账户' and direction = '收入'"));
	}
	/*详情页-结算账户-支出*/
	public function settleAccountOut(){
		return $this->account(array("where" => " and type = '员工结算账户' and direction = '支出'"));
	}
	/*详情页-账户列表
	$data = array(
		"where" => "查询条件",
	);
	*/
	public function account($data){
//		$this->get['page'] = "3";
//		$this->post['id'] = "SOg138131547gd";
		$staff = query("staff","stid = '".$this->post['id']."'");
		if(!$this->power("staff","see")){
			$json['warn'] = "您没有查询员工档案的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "员工ID为空";
		}elseif(empty($staff['stid'])){
			$json['warn'] = "未找到此员工";
		}else{
			//like查询数组
			$like = array(
				"text" => $this->post['text'],//备注
			);
			$and = "";
			//发生时间-开始日期
			if(!empty($this->post['startDay'])){
				$startDay = $this->post['startDay']." 00:00:00";
				$and .= " and time >= '$startDay' ";
			}
			//发生时间-结束日期
			if(!empty($this->post['endDay'])){
				$endDay = $this->post['endDay']." 23:59:59";
				$and .= " and time <= '$endDay' ";
			}
			//获取查询结果
			$where = " typeid = '".$staff['stid']."'".$data['where'].search(array("like" => $like)).$and;
			$page = $this->page(array("table" => "record","where" => $where,"order" => "id desc"));
			//数据分析
			$json['account'] = array();
			foreach($page['array'] as $array){
				if(empty($array['ico'])){
					$ico = "";
				}else{
					$ico = root.$array['ico'];
				}
				$json['account'][] = array(
					"id" => $array['id'],//账户记录ID
					"direction" => $array['direction'],//方向
					"money" => $array['money'],//金额
					"balance" => $array['balance'],//余额
					"text" => $array['text'],//备注
					"time" => $array['time'],//时间
					"ico" => $ico,//凭证
				);
			}
			$json['page'] = $page['para'];
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*员工详情页-账户详情页*/
	public function accountDetail(){
		$record = query("record","id = '".$this->post['id']."'");
		if(!$this->power("staff","see")){
			$json['warn'] = "您没有查询员工档案的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "账户记录ID为空";
		}elseif(empty($record['id'])){
			$json['warn'] = "未找到此账户记录";
		}else{
			if(empty($record['ico'])){
				$ico = "";
			}else{
				$ico = root.$record['ico'];
			}
			$json['detail'] = array(
				"direction" => $record['direction'],//方向
				"money" => $record['money'],//金额
				"balance" => $record['balance'],//余额
				"text" => $record['text'],//备注
				"time" => $record['time'],//时间
				"ico" => $ico,//凭证
			);
		}
		//返回
		return $json;
	}
}
?>